<%@Page Language="VB"%>

<%@Import Namespace="System.Data" %>
<%@ Register TagPrefix="wrox" TagName="connect" Src="..\global\connect-strings.ascx" %>
<%@ Register TagPrefix="wrox" TagName="getdataview" Src="..\global\get-dataview-control.ascx" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Sorting Rows and Finding Data in a DataGrid Control</title>
<style type="text/css">
body, td {font-family:Tahoma,Arial,sans-serif; font-size:10pt}
input {font-family:Tahoma,Arial,sans-serif; font-size:8pt}
.heading {font-family:Tahoma,Arial,sans-serif; font-size:14pt; font-weight:bold}
.subhead {font-family:Tahoma,Arial,sans-serif; font-size:12pt; font-weight:bold; padding-bottom:5px}
.cite {font-family:Tahoma,Arial,sans-serif; font-size:8pt}

.rHead {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial;
        font-size:14pt; font-weight:bold; padding:8px; color:green}
.rItem {font-family:Lucida Handwriting,Comic Sans MS,Tahoma,Arial,sans-serif;
        font-size:10pt}
.rFoot {font-family:Tahoma,Arial; font-size:8pt; padding:8px; color:darkgray}

</style></head>
<body bgcolor="#ffffff">
<span class="heading">Sorting Rows and Finding Data in a DataGrid Control</span><hr />
<!--------------------------------------------------------------------------->

<%'-- insert connection string script --%>
<wrox:connect id="ctlConnectStrings" runat="server" />

<%'-- insert the control that creates the DataSet --%>
<wrox:getdataview id="ctlDataView" runat="server" />

<form runat="server">

  Select only Titles containing the text:
  <ASP:TextBox id="txtFindText" runat="server" />
  <ASP:Button id="cmdFind" Text="Find" runat="server" /><p />

  <ASP:DataGrid id="MyDataGrid" runat="server"
       EnableViewState="False"
       CellPadding="5"
       GridLines="None"
       HeaderStyle-BackColor="silver"
       HeaderStyle-HorizontalAlign="center"
       FooterStyle-BackColor="silver"
       ShowFooter="True"
       AllowSorting="True"
       OnSortCommand="SortRows" />

</form>

<!--------------------------------------------------------------------------->

<script language="vb" runat="server">

Dim gstrSortOrder As String   'to hold the sort order
Dim gstrFindText As String    'to hold the filter expression

Sub Page_Load()
   If Page.IsPostback Then

      'set the value to be used for the RowFilter on the DataView
      gstrFindText = "Title LIKE '*" & txtFindText.Text & "*'"

   Else

      'set the default values for the sort string and filter text box
      gstrSortOrder = "ISBN"
      txtFindText.Text = "ASP"

   End If

   'recreate the data set and bind to the DataGrid control
   BindDataGrid()

End Sub


Sub SortRows(objSender As Object, objArgs As DataGridSortCommandEventArgs)
  'runs when the column headings in the DataGrid are clicked

  'get the sort expression (name of the column heading that was clicked)
  gstrSortOrder = objArgs.SortExpression.ToString()

  'recreate the data set and bind to the DataGrid control
  BindDataGrid()

End Sub


Sub BindDataGrid()

   'get connection string from ..\global\connect-strings.ascx user control
   Dim strConnect As String  = ctlConnectStrings.OLEDBConnectionString

   'create a SQL statement to select some rows from the database
   Dim strSelect As String
   strSelect = "SELECT * FROM BookList"

   'create a variable to hold an instance of a DataView object
   Dim objDataView As DataView

   'get DataView from get-dataview-control.ascx user control
   objDataView = ctlDataView.GetDataView(strConnect, strSelect)

   If IsNothing(objDataView) Then Exit Sub

   'sort the rows in the DataView into the specified order
   objDataView.Sort = gstrSortOrder

   'select the rows in the DataView that match the filter
   objDataView.RowFilter = gstrFindText

   'set the DataSource property of the DataList
   MyDataGrid.DataSource = objDataView

   'and bind the control to the data
   MyDataGrid.DataBind()

End Sub

</script>

<!--------------------------------------------------------------------------->
<hr /><span class="cite">[<a href="../global/viewsource.aspx">view source</a>] &nbsp; &nbsp; &nbsp; &nbsp;
&copy;2001 <a class="cite" href="http://www.wrox.com/">Wrox Press</a> -
<a class="cite" href="http://www.wrox.com/Books/Book_Details.asp?isbn=1861004885">Professional ASP.NET</a> (ISBN: 1-861004-88-5)</span>
</body>
</html>
